﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace PlanesManagerWebAPI.Controllers
{
    public class BookingsController : ApiController
    {
        public IHttpActionResult PostNewBooking(string FlightID, string CreditCardNumber, string ClientNameOnInvoice, string ClientDocumentID)
        {
            try
            {
                string token = Request.Headers.GetValues("token").FirstOrDefault();

                if (BusinessLoginLayer.Company_Details.ValidadeToken(token))
                {
                    CommonClasses.CompanyDetails companyDetails = BusinessLoginLayer.Company_Details.GetClientInfo(token);
                    CommonClasses.Flight flight = BusinessLoginLayer.Flights.GetFlightByID(FlightID);
                    int costumerID = KashFlowLayer.KashFlowManager.FindCostumerByEmail(companyDetails.Email);
                    if(costumerID == -1)
                        costumerID = KashFlowLayer.KashFlowManager.CreateCostumer(companyDetails.Name, companyDetails.Email);
                    string partyID = CRMLayer.CapsuleCRM.QueryPartyIDByEmail(companyDetails.Email);
                    if (string.IsNullOrEmpty(partyID))
                        CRMLayer.CapsuleCRM.AddCompany(companyDetails.Name, companyDetails.Email);

                    CommonClasses.Invoice invoice = KashFlowLayer.KashFlowManager.CreateInvoice(costumerID, ClientNameOnInvoice, ClientDocumentID, FlightID, decimal.Parse(flight.Price.ToString()));
                    Guid bookingID = BusinessLoginLayer.Bookings.NewBooking(token, FlightID, CreditCardNumber, ClientNameOnInvoice, ClientDocumentID, invoice.InvoiceID, invoice.InvoiceLink);
                    CRMLayer.CapsuleCRM.AddNewCase(companyDetails.Email, bookingID.ToString(), FlightID, ClientNameOnInvoice, ClientDocumentID, invoice.InvoiceID.ToString(), invoice.InvoiceLink);
                    CRMLayer.CapsuleCRM.AddOwnOpportunity(companyDetails.Email, bookingID.ToString(), FlightID, ClientNameOnInvoice, ClientDocumentID, invoice.InvoiceID.ToString(), flight.Price);

                    CommonClasses.Booking bookingResult = new CommonClasses.Booking(bookingID, FlightID, ClientNameOnInvoice, ClientDocumentID, invoice.InvoiceID.ToString(), invoice.InvoiceLink);
                    
                    return Ok(bookingResult);
                }
                else
                {
                    System.Net.Http.Headers.AuthenticationHeaderValue v = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic");
                    return Unauthorized(v);
                }
            }
            catch (Exception)
            {
                return InternalServerError();
            }
        }

        public IHttpActionResult GetBooking(string Id)
        {
            try
            {
                string token = Request.Headers.GetValues("token").FirstOrDefault();

                if (BusinessLoginLayer.Company_Details.ValidadeToken(token))
                {
                    CommonClasses.Booking booking = BusinessLoginLayer.Bookings.GetBooking(Id);
                    return Ok(booking);
                }
                else
                {
                    System.Net.Http.Headers.AuthenticationHeaderValue v = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic");
                    return Unauthorized(v);
                }
            }
            catch (Exception)
            {
                return InternalServerError();
            }
        }
    }
}
